﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.Common;
using System.Runtime.Serialization;
using SiGeSIB.Data.Common;
using System.Xml;

namespace SiGeSIB.Data.Security
{
    public class ConnectionSchema : IContentsSerializable
    {
		public ConnectionSchema() { }
        public ConnectionSchema(string connectionName, string login, string password, string dataSource, string database, bool isDefaultConnection)
        {
			IsDefaultConnection = isDefaultConnection;
			ConnectionName = connectionName;
			DatabaseName = database;
			DataSource = dataSource;
			Login = login;
			Password = password;
        }

		#region Attributes
		private const string XML_Id = "Id";
		private const string XML_IsDefaultConnection = "Default";
		private const string XML_ConnectionName = "ConnectionName";
		private const string XML_DataSource = "DataSource";
		private const string XML_Database = "Database";
		private const string XML_Login = "Login";
		private const string XML_Password = "Password";
		#endregion

		#region Properties
		public int Id { get; set; }
		public bool IsDefaultConnection { get; set; }
		public string ConnectionName { get; set; }
		public string DatabaseName { get; set; }
		public string DataSource { get; set; }
		public string Login { get; set; }
		public string Password { get; set; }
		#endregion

		#region IContentsSerializable Members

		public void Serialize(XmlWriter xmlw)
		{
			xmlw.WriteStartElement("ConnectionSchema");

			xmlw.WriteElementString(XML_Id, Id.ToString());
			xmlw.WriteElementString(XML_ConnectionName, ConnectionName);
			xmlw.WriteElementString(XML_Database, DatabaseName);
			xmlw.WriteElementString(XML_DataSource, DataSource);
			xmlw.WriteElementString(XML_Login, Login);
			xmlw.WriteElementString(XML_Password, Password);
			xmlw.WriteElementString(XML_IsDefaultConnection, IsDefaultConnection.ToString());

			xmlw.WriteEndElement();
		}

		public void Deserialize(XmlNode node)
		{
			if (node != null && node.Name == "ConnectionSchema")
			{
				if (node[XML_Id] != null)
					Id = int.Parse(node[XML_Id].InnerText);
				if (node[XML_IsDefaultConnection] != null)
					IsDefaultConnection = bool.Parse(node[XML_IsDefaultConnection].InnerText);
				if (node[XML_ConnectionName] != null)
					ConnectionName = node[XML_ConnectionName].InnerText;
				if (node[XML_DataSource] != null)
					DataSource = (node[XML_DataSource].InnerText);
				if (node[XML_Database] != null)
					DatabaseName = (node[XML_Database].InnerText);
				if (node[XML_Login] != null)
					Login = (node[XML_Login].InnerText);
				if (node[XML_Password] != null)
					Password = (node[XML_Password].InnerText);
			}
		}

		#endregion

		#region Methods
		public void TestConnection()
		{
			using (SqlConnection conn = CreateConnection())
			{
				conn.Close();
			}
		}
		public SqlConnection CreateConnection()
		{
			string connectionString = "user id={0};" +
									   "password={1};server={2};" +
									   "Trusted_Connection=no;" +
									   "database={3}; " +
									   "connection timeout=30";

			connectionString = string.Format(connectionString, Login, Password, DataSource, DatabaseName);

			SqlConnection conn = new SqlConnection(connectionString);
			conn.Open();

			return conn;
		}
		#endregion
	}
}
